package com.hongxun.design.activitys;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;

import com.baidu.mapapi.model.LatLng;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.adapters.ProjectNavigationAdapter;
import com.hongxun.design.bean.ProjectItemBean;
import com.hongxun.design.interfaces.HeadSecond;
import com.hongxun.design.interfaces.UpdateMap;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.listeners.MyLocationListenner;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.dialogstyle.MyDialog;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.ui.ui_listview.refreshlist.SwipeRefreshListView;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;
import com.hongxun.design.utils.maputil.NavigationUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/7/28.
 */
public class ProjectNavigationActivity extends BaseActivity implements HeadSecond, SwipeRefreshLayout.OnRefreshListener, UpdateMap {
    SwipeRefreshListView listview_project;
    int startNum = 0;
    int addNum =  MyApp.addNum;
    private Handler projectListHandler;
    List<ProjectItemBean> data;
    ProjectNavigationAdapter adapter;
    LatLng latLng;
    String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_projectnavigation);
        findBar();
        findView();
    }

    @Override
    public void findBar() {
        HeadSecondControlPanel headsecond_layout = (HeadSecondControlPanel) findViewById(R.id.navigation_layout);
        headsecond_layout.initHeadPanel(DataUtil.getsecondbean("", View.INVISIBLE, R.color.login_yellow, ListenerSet.back, "项目导航", R.color.title, View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));
    }

    @Override
    protected void onResume() {
        super.onResume();
        onRefresh();
    }

    @Override
    protected void findView() {
        super.findView();
        if (listview_project == null)
            listview_project = (SwipeRefreshListView) findViewById(R.id.listview_project);
        listview_project.setOnRefreshListener(this);
        addProjectListHandler();
        listview_project.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                MyDialog.Loading(mActivity);
                try {
                    latLng = new LatLng(Double.parseDouble(data.get(i).getLat()), Double.parseDouble(data.get(i).getLng()));
                } catch (Exception e) {
                    latLng = null;
                }
                name = data.get(i).getProject_name();
                if (MyApp.bdLocation != null) {
                    Navigation();
                } else {
                    ToastShow.showShort(mActivity, "获取当前位置！");

                    inItLocation();
                }
                MyDialog.Close();
            }
        });

    }

    @Override
    public void onRefresh() {
        startNum = 0;
        addNum =  MyApp.addNum;
        MyApp.HTTP.getProjectList(projectListHandler, HttpStringUtil.SUCCESSED, "",startNum, startNum + addNum);
    }


    @Override
    public void addMore() {
        super.addMore();
        MyApp.HTTP.getProjectList(projectListHandler, HttpStringUtil.ADDMROE,"", startNum, startNum + addNum);

    }

    private void addProjectListHandler() {
        projectListHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            if (data == null) {
                                data = new ArrayList<>();
                            } else {
                                data.clear();
                            }
                            data.addAll(FastJsonUtil.JsonToGetProjectListBean((String) msg.obj));
                            projectListHandler.sendEmptyMessage(1);
                            startNum = startNum + addNum;
                        } else {
                            listview_project.setRefreshing(false);
                            String s = FastJsonUtil.JsonToCheck_String((String) msg.obj);
                            ToastShow.showShort(mActivity, s);
                        }
                        break;
                    case 1://加载listview数据
                        if (adapter == null) {
                            MyLog.L(MString.SUCCESSED + "首次adapter " + adapter);
                            adapter = new ProjectNavigationAdapter(mActivity, data);
                            listview_project.setAdapter(adapter);
                            listview_project.setFragment(baseActivity);
                            listview_project.setCanScroll(true);
                            listview_project.setDivider(6);
                            listview_project.setRefreshing(false);
                        } else {
                            MyLog.L(MString.SUCCESSED + "adapter notifyDataSetChanged " + adapter);
                            //adapter.notifyDataSetChanged();
                            listview_project.update();
                            listview_project.setRefreshing(false);
                            listview_project.removeFooterView();
                        }
                        break;
                    case HttpStringUtil.ADDMROE:
                        int num = 0;
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            if (data == null) {
                                data = FastJsonUtil.JsonToGetProjectListBean((String) msg.obj);
                            } else {
                                num = Integer.parseInt(data.size() + "");
                                data.addAll(FastJsonUtil.JsonToGetProjectListBean((String) msg.obj));
                            }
                            projectListHandler.sendEmptyMessage(1);
                            if (num < data.size()) {
                                startNum = startNum + addNum;
                            } else {
                                ToastShow.showShort(mActivity, "没有数据了");
                            }
                        } else {
                            listview_project.setRefreshing(false);
                            String s = FastJsonUtil.JsonToCheck_String((String) msg.obj);
                            ToastShow.showShort(mActivity, s);
                        }
                        break;
                    case HttpStringUtil.ERROR:
                        ToastShow.showShort(mActivity, (String) msg.obj);
                        break;

                }
            }
        };
    }


    /**
     * 开启导航*
     */
    private void Navigation() {
        if (MyApp.bdLocation == null) {
            ToastShow.showShort(mActivity, "定位失败！");
            return;
        } else if (latLng == null || latLng.longitude <= 0 || latLng.latitude <= 0) {
            ToastShow.showShort(mActivity, "获取项目位置失败！");
            return;
        } else {
            ToastShow.showShort(mActivity, "正在启动导航！");
            MyLog.L(MString.SUCCESSED + " 开始位置：" + MyApp.bdLocation.getLatitude() + "----" + MyApp.bdLocation.getLongitude());
            MyLog.L(MString.SUCCESSED + " 结束位置：" + latLng.latitude + "----" + latLng.longitude);
            NavigationUtil.startNavi(this,new LatLng(MyApp.bdLocation.getLatitude(), MyApp.bdLocation.getLongitude()), latLng, MyApp.bdLocation.getAddrStr(), name);
        }
    }

    /**
     * 开启定位图层*
     */
    private void inItLocation() {
        MyLocationListenner myListener = MyLocationListenner.getint(this);
        myListener.setIsFirstLoc(true);
        myListener.setIsLocal(true);

    }

    @Override
    public void updateMapState(LatLng latLng) {
        Navigation();
    }
}
